package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.dao.wills.MemberAmortizeDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.util.ExportXlsx;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;

import com.aidong.web.controller.BaseController;
import org.springframework.stereotype.Service;

@Service
public class MemberAmortizeService extends BaseController {
    @Resource
    MemberAmortizeDao memberAmortizeDao;

    public MemberAmortizeService() {
    }

    public boolean createAmortizeDetail(String period) {
        Date dt = this.parseDate(period + "01", "yyyyMMdd");
        if(dt == null) {
            return false;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年M月");
            Date dt1 = this.parseDate("1800-01-01", sdf);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            int year = cal.get(1);
            String ym = sdf1.format(dt);
            cal.add(2, 1);
            dt = cal.getTime();
            cal.setTime(this.parseDate("2007-01-01", sdf));
            try {
                String e = this.getSetting("downloadPath") + year + File.separator + "amortize";
                File dir = new File(e);
                if(!dir.exists()) {
                    dir.mkdirs();
                }
                ExportXlsx xls = new ExportXlsx(e + File.separator + "amortize_" + period + ".xlsx", "总表");
                xls.addHead(new String[]{"门店编号", "门店名称", ym + "摊销", "累计摊销"});
                xls.setWidth(new int[]{10, 12, 15, 15});
                List list = this.memberAmortizeDao.getAmortizeSum(period);
                Iterator sheetName = list.iterator();

                while(sheetName.hasNext()) {
                    ContractInfo i$ = (ContractInfo)sheetName.next();
                    xls.addRow(new Object[]{i$.getShopCode(), this.cache.getValue("shopName", i$.getShopCode()), i$.getFactPrice(), i$.getTotalPrice()});
                }
                while(cal.getTime().before(dt)) {
                    String sheetName1 = String.valueOf(cal.get(1)).substring(2) + "年合同";
                    xls.addSheet(sheetName1);
                    xls.addHead(new String[]{"门店编号", "门店名称", "合同号", "会员号", "销售类型", "销售日期", "开始日期", "结束日期", "提前终止日期", "类型", "卡种", "总次数", "剩余次数", "收款金额", "转入金额", "转出金额", "一次性摊销", "本月摊销", "累计摊销"});
                    xls.setWidth(new int[]{10, 12, 15, 18, 10, 12, 12, 12, 12, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10});
                    cal.add(1, 1);
                    if(cal.getTime().after(dt)) {
                        cal.setTime(dt);
                    }

                    list = this.memberAmortizeDao.getAmortizeDetail(period, dt1, cal.getTime());
                    Iterator i$1 = list.iterator();

                    while(i$1.hasNext()) {
                        ContractInfo contract = (ContractInfo)i$1.next();
                        xls.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), contract.getContractCode(), contract.getMemberCode(), this.cache.getValue("memberSaleType", contract.getSaleType()), sdf.format(contract.getSaleDate()), contract.getBeginTime() == null?null:sdf.format(contract.getBeginTime()), contract.getEndTime() == null?null:sdf.format(contract.getEndTime()), contract.getEffectiveDate() == null?null:sdf.format(contract.getEffectiveDate()), "01".equals(contract.getType())?"时段卡":"次卡", this.cache.getValue("cardType", contract.getCardType()), contract.getTotalCount(), contract.getRestCount(), contract.getPayAmount(), contract.getInAmount(), contract.getOutAmount(), contract.getFixedFee(), contract.getFactPrice(), contract.getTotalPrice()});
                    }

                    dt1 = cal.getTime();
                }

                xls.close();
                return true;
            } catch (Exception var16) {
                return false;
            }
        }
    }
}
